import random
import time
import pymysql

db = pymysql.connect(host="localhost", user="root", password="root", database="dedecmsv57utf8sp2")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

arcrank = -1   #审核状态-1 未审核 0 已审核
typeid = 1    #栏目ID
typeid2 = 0   #副栏目
channel = 1   #模型
senddate = int(time.time())
sortrank = int(time.time())
mid = 1
sql = "INSERT INTO dede_arctiny (arcrank,typeid,typeid2,channel,senddate, sortrank, mid) VALUES (%s,%s,%s,%s,%s,%s,%s)"
args_tiny = (arcrank, typeid, typeid2, channel, senddate, sortrank, mid)
print(args_tiny)
insert1 = cursor.execute(sql, args_tiny)
lastid = db.insert_id() #获取lastid

# //保存到主表
flag = ''
ismake = 0
click = random.randrange(500,1000)
money = 0
title = '标题'
shorttitle = '短标题'
color = ''
writer = '网友'
source = '未知'
litpic = ''
pubdate = int(time.time())
senddate = int(time.time())
voteid = 0
notpost = 0
description = '描述'
keywords = '关键词'
filename = ''
dutyadmin = 1
weight = lastid
sql_article = "INSERT INTO dede_archives(id,typeid,typeid2,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,color,writer,source,litpic,pubdate,senddate,mid,voteid,notpost,description,keywords,filename,dutyadmin,weight) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
args_article = (lastid, typeid, typeid2, sortrank, flag, ismake, channel, arcrank, click, money, title, shorttitle, color, writer, source, litpic, pubdate, senddate, mid, voteid, notpost, description, keywords, filename, dutyadmin, weight)
try:
    insert2 = cursor.execute(sql_article, args_article)
except:
    sql_del_tiny = "delete from dede_arctiny where id="+str(lastid)
    d = cursor.execute(sql_del_tiny)
    db.commit()

#插入到dede_addonarticle
aid = lastid
redirecturl = ''
templet = ''
userip = '8.8.8.8'
body = '<html>123123</html>'
sql_body= "INSERT INTO dede_addonarticle(aid,typeid,redirecturl,templet,userip,body) Values(%s,%s,%s,%s,%s,%s)"
args_body = (aid,typeid,redirecturl,templet,userip,body)
try:
    insert3 = cursor.execute(sql_body, args_body)
except:
    sql_del_tiny = "delete from dede_arctiny where id="+str(lastid)
    d = cursor.execute(sql_del_tiny)
    sql_del_article = "delete from dede_archives where id=" + str(lastid)
    d = cursor.execute(sql_del_article)
    db.commit()

cursor.close()
db.close()